package com.way.fileexplorer.activities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import android.app.ActionBar;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.way.fileexplorer.R;
import com.way.fileexplorer.util.ActivityUtils;
import com.way.fileexplorer.util.FileSearch;
import com.way.fileexplorer.util.FileType;
import com.way.fileexplorer.util.StorageUtil;

public class FileSearchActivity extends Activity implements
		View.OnClickListener {

	private String mSearchKey;
	private CheckBox mImageType;
	private CheckBox mVideoType;
	private CheckBox mAudioType;
	private CheckBox mApkType;
	private CheckBox mOthertype;
	private CheckBox mDocType;
	//private Spinner mSpinner;
	private int mSearchLocationType;
	private Set<Integer> mSearchType = new HashSet<Integer>();
	private RelativeLayout mSearchViewContainer;
	private EditText mSearchView;
	private ImageView mClearAll;

	private View mSearchLoactionLayout;
	private TextView mSearchLocationTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_search_view);
		mSearchViewContainer = (RelativeLayout) findViewById(R.id.search_view_container);
		if (mSearchViewContainer != null) {
			mSearchView = (EditText) findViewById(R.id.search_view);
			mSearchView.setOnEditorActionListener(new OnEditorActionListener() {
				@Override
				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					mSearchKey = v.getText().toString();
					if (mSearchKey != null && !mSearchKey.isEmpty()) {
					    if(mSearchKey.contains("*") || mSearchKey.contains("\"") ||  mSearchKey.contains("\\") || 
					            mSearchKey.contains("/") || mSearchKey.contains("?") || mSearchKey.contains("|")
		                        || mSearchKey.contains("|") || mSearchKey.contains("<") || mSearchKey.contains(">") || mSearchKey.contains("\'")){
		                    String invalidchar = "*\":/?|<>";
		                    Toast.makeText(FileSearchActivity.this, getResources().getString(
                                    R.string.invalid_char,invalidchar), Toast.LENGTH_SHORT).show();
		                    return true;
					    }
						Bundle bun = new Bundle();
						bun.putString(FileSearch.SEARCH_KEY, mSearchKey);
						bun.putInt(FileSearch.SEARCH_LOCATION,
								mSearchLocationType);
						bun.putIntegerArrayList(FileSearch.SEARCH_TYPE,
								new ArrayList<Integer>(mSearchType));
						ActivityUtils.startSearchMode(bun,
								FileSearchActivity.this);
						finish();
					} else {
						Toast.makeText(FileSearchActivity.this, R.string.search_empty, Toast.LENGTH_SHORT).show();
					}
					return true;
				}
			});
			mSearchView.addTextChangedListener(mTextWatcher);
			mSearchView.clearFocus();
			mClearAll = (ImageView) findViewById(R.id.clear_all_img);
			mClearAll.setOnClickListener(this);
			ActionBar actionbar = getActionBar();
			actionbar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
		}

		mImageType = (CheckBox) findViewById(R.id.fragment_search_type_image);
		mVideoType = (CheckBox) findViewById(R.id.fragment_search_type_vedio);
		mAudioType = (CheckBox) findViewById(R.id.fragment_search_type_audio);
		mApkType = (CheckBox) findViewById(R.id.fragment_search_type_apks);
		mDocType = (CheckBox) findViewById(R.id.fragment_search_type_document);
		mOthertype = (CheckBox) findViewById(R.id.fragment_search_type_other);
		mSearchLoactionLayout = findViewById(R.id.fragment_search_location_parent);
		mSearchLocationTextView = (TextView)findViewById(R.id.fragment_search_location_type);
		mImageType.setOnClickListener(this);
		mVideoType.setOnClickListener(this);
		mAudioType.setOnClickListener(this);
		mApkType.setOnClickListener(this);
		mDocType.setOnClickListener(this);
		mOthertype.setOnClickListener(this);
		if(StorageUtil.getInternalStorageState().equals("mounted") && StorageUtil.getExternalStorageState().equals("mounted")){
		    mSearchLoactionLayout.setClickable(true);
		    mSearchLoactionLayout.setOnClickListener(this);
		    mSearchLocationTextView.setText(R.string.title_section_all);
		}else{
		    mSearchLoactionLayout.setClickable(false);
		    mSearchLocationTextView.setText(R.string.title_section_internal);
		}
		

		//init Data
		mImageType.setChecked(true);
		mSearchType.add(FileType.FILE_TYPE_IMAGE);
		setSearchLoaction(0);
	}

	TextWatcher mTextWatcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence queryString, int start,
				int before, int count) {
		     if(queryString.length() >= 84){
                 Toast.makeText(FileSearchActivity.this, R.string.length_limited, Toast.LENGTH_SHORT).show();
             }
			if (!TextUtils.isEmpty(queryString.toString())) {
				if (mClearAll != null) {
					mClearAll.setVisibility(View.VISIBLE);
				}
			} else {
				if (mClearAll != null) {
					mClearAll.setVisibility(View.GONE);
				}
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fragment_search_type_image:
			if(mSearchType.contains(FileType.FILE_TYPE_IMAGE)){
				mSearchType.remove(FileType.FILE_TYPE_IMAGE);
			}else{
				mSearchType.add(FileType.FILE_TYPE_IMAGE);
			}
			break;
		case R.id.fragment_search_type_vedio:
			if(mSearchType.contains(FileType.FILE_TYPE_VIDEO)){
				mSearchType.remove(FileType.FILE_TYPE_VIDEO);
			}else{
				mSearchType.add(FileType.FILE_TYPE_VIDEO);
			}
			break;
		case R.id.fragment_search_type_audio:
			if(mSearchType.contains(FileType.FILE_TYPE_AUDIO)){
				mSearchType.remove(FileType.FILE_TYPE_AUDIO);
			}else{
				mSearchType.add(FileType.FILE_TYPE_AUDIO);
			}
			break;
		case R.id.fragment_search_type_document:
			if(mSearchType.contains(FileType.FILE_TYPE_DOC)){
				mSearchType.remove(FileType.FILE_TYPE_DOC);
			}else{
				mSearchType.add(FileType.FILE_TYPE_DOC);
			}
			break;
		case R.id.fragment_search_type_apks:
			if(mSearchType.contains(FileType.FILE_TYPE_PACKAGE)){
				mSearchType.remove(FileType.FILE_TYPE_PACKAGE);
			}else{
				mSearchType.add(FileType.FILE_TYPE_PACKAGE);
			}
			break;
		case R.id.fragment_search_type_other:
			if(mSearchType.contains(FileType.FILE_TYPE_UNKNOE)){
				mSearchType.remove(FileType.FILE_TYPE_UNKNOE);
			}else{
				mSearchType.add(FileType.FILE_TYPE_UNKNOE);
			}
			break;
		case R.id.clear_all_img:
			if (mSearchView != null && mClearAll != null) {
				mSearchView.setText("");
				mClearAll.setVisibility(View.GONE);
			}
			break;
		case R.id.fragment_search_location_parent:
			ActivityUtils.showSearchLocationDialog(FileSearchActivity.this,new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					setSearchLoaction(which);
				}
			});
			break;
		}
	}
	
	public void setSearchLoaction(int position) {
		int iTextViewId;
		if (getString(R.string.title_section_internal).equals(ActivityUtils.getSearchLocation(position))) {
			mSearchLocationType = FileSearch.SEARCH_INTERNAL;
			iTextViewId = R.string.title_section_internal;
		} else if (getString(R.string.title_section_external).equals(
				ActivityUtils.getSearchLocation(position))) {
			mSearchLocationType = FileSearch.SEARCH_EXTERNAL;
			iTextViewId = R.string.title_section_external;
		} else {
			mSearchLocationType = FileSearch.SEARCH_ALL;
			iTextViewId = R.string.title_section_all;
		}
		mSearchLocationTextView.setText(iTextViewId);
	}
	   @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	        case android.R.id.home:
	            onBackPressed();
	            return true;
	        }
	        return false;
	    }
}
